package com.ktgj.customer.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.customer.domain.*;
import com.ktgj.customer.domain.dto.BackstageDTO;
import com.ktgj.customer.domain.vo.MallProductImgVO;
import com.ktgj.customer.domain.vo.MallProductSpecVO;
import com.ktgj.customer.domain.vo.MallProductVO;
import com.ktgj.customer.service.IMallCategoryService;
import com.ktgj.customer.service.IMallProductImgService;
import com.ktgj.customer.service.IMallProductSpecService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.MallProductMapper;
import com.ktgj.customer.service.IMallProductService;

/**
 * 商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-05-30
 */
@Service
public class MallProductServiceImpl implements IMallProductService 
{
    @Autowired
    private MallProductMapper mallProductMapper;
    @Autowired
    private IMallProductSpecService mallProductSpecService;
    @Autowired
    private IMallProductImgService mallProductImgService;
    @Autowired
    private IMallCategoryService mallCategoryService;

    /**
     * 查询商品
     * 
     * @param productId 商品ID
     * @return 商品
     */
    @Override
    public MallProduct selectMallProductById(Long productId)
    {
        return mallProductMapper.selectMallProductById(productId);
    }

    /**
     * 查询商品列表
     * 
     * @param mallProduct 商品
     * @return 商品
     */
    @Override
    public List<MallProduct> selectMallProductList(MallProduct mallProduct)
    {
        return mallProductMapper.selectMallProductList(mallProduct);
    }

    /**
     * 新增商品
     * 
     * @param mallProduct 商品
     * @return 结果
     */
    @Override
    public int insertMallProduct(MallProduct mallProduct)
    {
        mallProduct.setCreateTime(DateUtils.getNowDate());
        return mallProductMapper.insertMallProduct(mallProduct);
    }

    /**
     * 修改商品
     * 
     * @param mallProduct 商品
     * @return 结果
     */
    @Override
    public int updateMallProduct(MallProduct mallProduct)
    {
        mallProduct.setUpdateTime(DateUtils.getNowDate());
        return mallProductMapper.updateMallProduct(mallProduct);
    }

    /**
     * 批量删除商品
     * 
     * @param productIds 需要删除的商品ID
     * @return 结果
     */
    @Override
    public int deleteMallProductByIds(Long[] productIds)
    {
        return mallProductMapper.deleteMallProductByIds(productIds);
    }

    /**
     * 删除商品信息
     * 
     * @param productId 商品ID
     * @return 结果
     */
    @Override
    public int deleteMallProductById(Long productId)
    {
        return mallProductMapper.deleteMallProductById(productId);
    }

    //商城首页
    @Override
    public MallHomePage mallHomepage() {
        MallHomePage mallHomePage = new MallHomePage();
        List<MallProductVO> productVOList = mallProductMapper.mallHomepage();
        productVOList.forEach(item -> {
            MallProductVO productVO = new MallProductVO();
            if (item.getTypeId() != null && item.getTypeId() > 0) {
                if (item.getTypeId() == 1) {
                    BeanUtils.copyProperties(item, productVO);
                    List<MallProductSpecVO> mallProductSpecVOS = mallProductSpecService.mallProductSpecList(item.getProductId());
                    if (mallProductSpecVOS != null && mallProductSpecVOS.size() > 0) {
                        BeanUtils.copyProperties(mallProductSpecVOS.get(0), productVO);
                    }
                    mallHomePage.getHotMallProduct().add(productVO);
                } else if (item.getTypeId() == 2) {
                    BeanUtils.copyProperties(item, productVO);
                    List<MallProductSpecVO> mallProductSpecVOS = mallProductSpecService.mallProductSpecList(item.getProductId());
                    if (mallProductSpecVOS != null && mallProductSpecVOS.size() > 0) {
                        BeanUtils.copyProperties(mallProductSpecVOS.get(0), productVO);
                    }
                    mallHomePage.getExchangeMallProduct().add(productVO);
                } else if (item.getTypeId() == 3) {
                    BeanUtils.copyProperties(item, productVO);
                    List<MallProductSpecVO> mallProductSpecVOS = mallProductSpecService.mallProductSpecList(item.getProductId());
                    if (mallProductSpecVOS != null && mallProductSpecVOS.size() > 0) {
                        BeanUtils.copyProperties(mallProductSpecVOS.get(0), productVO);
                    }
                    mallHomePage.getNewMallProduct().add(productVO);
                }
            }
        });
        return mallHomePage;
    }

    /**
     * 分类展示
     * @param id
     * @param cId
     * @return
     */
    @Override
    public List<MallProductVO> selectMallProduct(Long id, Long cId) {
        List<MallProductVO> productVOS = mallProductMapper.selectMallProduct(id);
        List<MallProductVO> productVOList = new ArrayList<>();
        productVOS.forEach(item -> {
            List<MallProductSpecVO> mallProductSpecVOS = mallProductSpecService.mallProductSpecList(item.getProductId());
            if (mallProductSpecVOS != null && mallProductSpecVOS.size() > 0) {
                BeanUtils.copyProperties(mallProductSpecVOS.get(0), item);
            }
        });
        if (cId == 0) {
            return productVOS;
        } else {
            productVOS.forEach(item -> {
                if (cId == item.getCategoryCid())
                    productVOList.add(item);
            });
            return productVOList;
        }
    }

    //商品搜索
    @Override
    public List<MallProductVO> productSearch(String type, String productName) {
        List<MallProductVO> productVOList = mallProductMapper.productSearch(type, productName);
        productVOList.forEach(item -> {
            List<MallProductSpecVO> mallProductSpecVOS = mallProductSpecService.mallProductSpecList(item.getProductId());
            if (mallProductSpecVOS != null && mallProductSpecVOS.size() > 0) {
                BeanUtils.copyProperties(mallProductSpecVOS.get(0), item);
            }
        });
        return productVOList;
    }

    //商品详情
    @Override
    public MallProductDetails mallProductDetails(Long id) {
        MallProductDetails details = new MallProductDetails();
        MallProduct mallProduct = mallProductMapper.selectMallProductById(id);
        BeanUtils.copyProperties(mallProduct, details);
        MallProductSpec spec = new MallProductSpec();
        spec.setProductId(mallProduct.getProductId());
        List<MallProductSpec> mallProductSpecs = mallProductSpecService.selectMallProductSpecList(spec);
        if (mallProductSpecs != null && mallProductSpecs.size() > 0) {
            mallProductSpecs.forEach(item -> {
                if (item.getDeleteTime() == null) {
                    MallProductSpecVO specVO = new MallProductSpecVO();
                    BeanUtils.copyProperties(item, specVO);
                    details.getProductSpecList().add(specVO);
                }
            });
        }
        MallProductImg productImg = new MallProductImg();
        productImg.setProductId(mallProduct.getProductId());
        List<MallProductImg> mallProductImgs = mallProductImgService.selectMallProductImgList(productImg);
        if (mallProductImgs != null && mallProductImgs.size() > 0) {
            mallProductImgs.forEach(item -> {
                String img = item.getImgPath();
                details.getImgPath().add(img);
            });
        }
        return details;
    }

    //后台商品列表
    @Override
    public List<MallProductBackstage> backstage(BackstageDTO backstageDTO) {
        if (backstageDTO.isSellOut()) {
            backstageDTO.setStockCount(0L);
        }
        List<MallProductBackstage> backstage = mallProductMapper.backstage(backstageDTO);
        backstage.forEach(item -> {
            if (item.getTypeId() != null) {
                String typeName = mallProductMapper.selectTypeName(item.getTypeId());
                item.getProductType().add(typeName);
            }
            if (item.getCategoryPid() != null) {
                MallCategory mallCategory = mallCategoryService.selectMallCategoryById(item.getCategoryPid());
                if (mallCategory != null)
                    item.getProductType().add(mallCategory.getName());
            }
            if (item.getCategoryCid() != null) {
                MallCategory mallCategory = mallCategoryService.selectMallCategoryById(item.getCategoryCid());
                if (mallCategory != null)
                    item.getProductType().add(mallCategory.getName());
            }
            MallProductSpec spec = new MallProductSpec();
            spec.setProductId(item.getProductId());
            List<MallProductSpec> mallProductSpecs = mallProductSpecService.selectMallProductSpecList(spec);
            if (mallProductSpecs != null && mallProductSpecs.size() > 0) {
                if (backstageDTO.isSellOut()) {
                    mallProductSpecs.forEach(items -> {
                        if (items.getStockCount() == 0) {
                            item.setMarketPrice(items.getMarketPrice());
                            item.setMemberPrice(items.getMemberPrice());
                            item.setOriginalPrice(items.getOriginalPrice());
                            item.setSalePrice(items.getSalePrice());
                            item.setStockCount(items.getStockCount());
                        }
                    });
                } else {
                    item.setMarketPrice(mallProductSpecs.get(0).getMarketPrice());
                    item.setMemberPrice(mallProductSpecs.get(0).getMemberPrice());
                    item.setOriginalPrice(mallProductSpecs.get(0).getOriginalPrice());
                    item.setSalePrice(mallProductSpecs.get(0).getSalePrice());
                    item.setStockCount(mallProductSpecs.get(0).getStockCount());
                }
            }
            if (item.getIfpass().equals("0"))
                item.setIfpass("未审");
            else
                item.setIfpass("已审");
        });
        return backstage;
    }

    //商品类型
    @Override
    public List<MallProductType> mallProductTypeList() {
        return mallProductMapper.mallProductTypeList();
    }

    //后台商品详情
    @Override
    public MallProductBackstageDetails productBackstageDetails(Long productId) {
        MallProductBackstageDetails details = new MallProductBackstageDetails();
        MallProduct mallProduct = mallProductMapper.selectMallProductById(productId);
        BeanUtils.copyProperties(mallProduct, details);
        List<MallProductSpecVO> specList = mallProductSpecService.mallProductSpecList(productId);
        if (specList != null && specList.size() > 0) {
            specList.forEach(item -> {
                MallProductSpecVO specVO = new MallProductSpecVO();
                BeanUtils.copyProperties(item, specVO);
                details.getMallProductSpec().add(specVO);
            });
        }
        List<MallProductImg> mallProductImgs = mallProductImgService.queryMallProductImgByProductId(productId);
        if (mallProductImgs != null && mallProductImgs.size() > 0) {
            for (MallProductImg mallProductImg : mallProductImgs) {
                details.getImgPaths().add(mallProductImg.getImgPath());
            }
        }
        return details;
    }

    //修改后台商品
    @Override
    public int updateMallProduct(MallProductBackstageDetails backstageDetails) {
        MallProductBackstageDetails details = productBackstageDetails(backstageDetails.getProductId());
        MallProduct mallProduct = new MallProduct();
        BeanUtils.copyProperties(backstageDetails, mallProduct);
        if (details.getMallProductSpec().size() == backstageDetails.getMallProductSpec().size()) {
            backstageDetails.getMallProductSpec().forEach(item -> {
                MallProductSpec productSpec = new MallProductSpec();
                BeanUtils.copyProperties(item, productSpec);
                productSpec.setProductId(backstageDetails.getProductId());
                productSpec.setUpdateTime(new Date());
                mallProductSpecService.updateMallProductSpec(productSpec);
            });
        } else if (details.getMallProductSpec().size() < backstageDetails.getMallProductSpec().size()) {
            backstageDetails.getMallProductSpec().forEach(item -> {
                MallProductSpec productSpec = new MallProductSpec();
                if (item.getProductSpecId() == 0) {
                    BeanUtils.copyProperties(item, productSpec);
                    productSpec.setProductId(backstageDetails.getProductId());
                    productSpec.setCreateTime(new Date());
                    productSpec.setUpdateTime(new Date());
                    mallProductSpecService.insertMallProductSpec(productSpec);
                } else {
                    BeanUtils.copyProperties(item, productSpec);
                    productSpec.setProductId(backstageDetails.getProductId());
                    productSpec.setUpdateTime(new Date());
                    mallProductSpecService.updateMallProductSpec(productSpec);
                }
            });
        } else if (details.getMallProductSpec().size() > backstageDetails.getMallProductSpec().size()) {
            List<Long> detailsLong = new ArrayList<>();
            List<Long> backstageLong = new ArrayList<>();
            details.getMallProductSpec().forEach(item -> {
                Long id = item.getProductSpecId();
                detailsLong.add(id);
            });
            backstageDetails.getMallProductSpec().forEach(items -> {
                Long id = items.getProductSpecId();
                backstageLong.add(id);
                MallProductSpec productSpec = new MallProductSpec();
                BeanUtils.copyProperties(items, productSpec);
                productSpec.setProductId(backstageDetails.getProductId());
                productSpec.setUpdateTime(new Date());
                mallProductSpecService.updateMallProductSpec(productSpec);
            });
            Collection tion = new ArrayList(detailsLong);
            tion.removeAll(backstageLong);
            tion.forEach(item -> {
                MallProductSpec productSpec = new MallProductSpec();
                productSpec.setProductSpecId((Long) item);
                productSpec.setDeleteTime(new Date());
                mallProductSpecService.updateMallProductSpec(productSpec);
            });
        }
        if (backstageDetails.getImgPaths().size() > 0) {
            MallProductImgVO productImgVO = new MallProductImgVO();
            productImgVO.setProductId(backstageDetails.getProductId());
            productImgVO.getImgPaths().addAll(backstageDetails.getImgPaths());
            mallProductImgService.insertProductImg(productImgVO);
        }
        return mallProductMapper.updateMallProduct(mallProduct);
    }

    //添加后台商品
    @Override
    public int addMallProduct(MallProduct product) {
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        int mallProduct = mallProductMapper.insertMallProduct(product);
        if (product.getMallProductSpec().size() > 0) {
            product.getMallProductSpec().forEach(item -> {
                MallProductSpec productSpec = new MallProductSpec();
                BeanUtils.copyProperties(item, productSpec);
                productSpec.setProductId(product.getProductId());
                productSpec.setCreateTime(new Date());
                productSpec.setUpdateTime(new Date());
                mallProductSpecService.insertMallProductSpec(productSpec);
            });
        }
        if (product.getImgPaths().size() > 0) {
            MallProductImgVO productImgVO = new MallProductImgVO();
            productImgVO.setProductId(product.getProductId());
            productImgVO.getImgPaths().addAll(product.getImgPaths());
            mallProductImgService.insertProductImg(productImgVO);
        }
        return mallProduct;
    }

    //后台删除商品
    @Override
    public int deleteMallProduct(Long productId) {
        MallProductBackstageDetails details = productBackstageDetails(productId);
        MallProduct product = new MallProduct();
        product.setProductId(productId);
        product.setDeleteTime(new Date());
        if (details.getMallProductSpec() != null && details.getMallProductSpec().size() > 0) {
            details.getMallProductSpec().forEach(item -> {
                MallProductSpec productSpec = new MallProductSpec();
                productSpec.setProductId(productId);
                productSpec.setDeleteTime(new Date());
                mallProductSpecService.updateMallProductSpecByProId(productSpec);
            });
        }
        return mallProductMapper.updateMallProduct(product);
    }

    //后台添加活动商品列表
    @Override
    public List<MallActivityProductADD> activityProduct(Long typeId, Long propertyTypeId, String productName) {
        List<MallActivityProductADD> productADDList = mallProductMapper.activityProduct(typeId, propertyTypeId, productName);
        if (productADDList != null && productADDList.size() > 0) {
            productADDList.forEach(item -> {
                List<MallActivityProductSpec> productSpecList = mallProductSpecService.productSpecs(item.getProductId());
                item.getProductSpecs().addAll(productSpecList);
            });
        }
        return productADDList;
    }
}
